// JavaScript Document


//提交函数;
function sub_mit(sub){
		//注册检测：
		//用户提交检验:
		if(!acc_sub()){ 
			alert ("用户名不符合要求!");
			return false;
		}
		//密码提交检验:
		if(!pwd1_sub()){ 
			alert ("密码不符合要求!");
			return false;
		}
		//2次密码提交校验:
		if(!chkAgain()){ 
			alert ("密码确认不符合要求!");
			return false;
		}
		if(!areaCHK()){
			alert("请选择地区!");
			return false;
		}
		if(!verifyCHK()){
			alert("验证码不能为空!");
			return false;
		}
}



//登陆传递log 注册传递reg
function set_op(op){ 
	
	var x = $("#oper")[0];
	x.value = "";
	if(op.value == "登录"){
		x.value = "log";
		/*alert (x.value);*/
	}
	else if(op.value == "注册"){
		x.value = "reg";
		/*alert (x.value);*/
	}
	else if(op.value == "注销"){
		x.value = "log_out";
		/*alert (x.value);*/
	}	

}


//校验用户名是否为空:
function chk_acc(){
	var x = $("#acc_name")[0];
/*	alert ($("#acc_chked")[0].value);*/
	
		if(x.value == ""){ 
			$("#acc_chked")[0].value = 0;
			acc_sub();
		}
		else {
			$("#acc_result")[0].innerHTML = "";
			/*alert (x.value);*/
			$.post("chk_acc",{acc:x.value},function (s){
															/*var acc_arr = eval(s);*/
															
															if (s > 0){
																$("#acc_chked")[0].value = 1;
																acc_sub();
																
															}
															else {
																$("#acc_chked")[0].value = 2;
															}
														})
			
		}
}

function acc_sub(){
	switch ($("#acc_chked")[0].value){
		case "0":
			$("#acc_result")[0].innerHTML = "姓名不能为空";
			return false;
		case "1":
			$("#acc_result")[0].innerHTML =	"姓名不能重复";
			return false;
		case "2":
			return true;
	}
}




//密码校验：
function clear_text(a){
	
	if(a.value == ""){
		document.getElementById("Result").style.color = "gray";
		document.getElementById("Result").style.textIndent="";
		document.getElementById("Result").style.fontSize = "11px";
		document.getElementById("Result").innerHTML = "";
		document.getElementById("Result").style.background ="";
	}

}


var pwdChecked = 0;

function chkAgain(){
	var chk_pwd1 = $("#pwd")[0];
	var chk_pwd2 = $("#pwd2")[0];
	
	if (chk_pwd2.value =="" && chk_pwd1.value !=""){
		pwdChecked = 0;
		$("#pwd2_chked")[0].value = 1;
		$("#pwd_again")[0].innerHTML = "请重复输入密码！";
		return false;
	}
	else if (chk_pwd2.value != chk_pwd1.value){
		pwdChecked++;
		$("#pwd_again")[0].innerHTML = "两次密码不一致请重新输入！";
		return false;
/*	document.form1.pwd_re.focus();
	document.form1.pwd_re.select();*/
	}
	else { 
		pwdChecked = 0;
		$("#pwd_again")[0].innerHTML = "";
		return true;
	}
}

function rePwd(pwd2){

	if (pwdChecked != 0 ){
		pwd2.focus();
		pwd2.select();
	}
}


function chkPwd(obj){
	var pwd_in = obj.value;
	/*	alert (pwd_in);*/

	$("#pwd1_chked")[0].value = getChk(pwd_in);
	/*alert ($("#pwd1_chked")[0].value);*/
   /*alert ("id="+id);*/
	pwd1_sub();
}

function getChk (i)
{
/*  alert (i.length);*/
	if (i.length < 4){
		return 0;
	}

	var li = 0;

	if (i.match(/[a-z]/ig)){
/*	alert ("a-z li="+li);*/
		li++;
	}
	if (i.match(/[0-9]/ig)){
/*	alert ("0-9 li="+li);*/
		li++;
	}
	if (i.match(/(.[^a-z0-9])/ig) !==""){
/*	alert ("a-z0-9 li="+li);*/
		li++;
	}
	if (i.length < 6 && li > 0)	{
/*	alert ("li="+li);*/
	li--;
	}
	return li
}

function pwd1_sub(){
	/*alert (document.getElementById("pwd1_chked").value);*/
	var msg=new Array(4);
	var id = $("#pwd1_chked")[0].value;
	
	msg[0]="密码过短。";
	msg[1]="密码强度差。";
	msg[2]="密码强度良好。";
	msg[3]="密码强度高。";

	
	var len = Array ()
	len[0] = -45;
	len[1] = -30;
	len[2] = -15;
	len[3] = 0; 
	
	var color = Array ()
	color[0] = "gray";
	color[1] = "red";
	color[2] = "#ff6600";
	color[3] = "green"; 
	
	var bImg = path;
	
	var sWidth=180;
	var sHeight=15;
	var Bobj=document.getElementById("pwd_result");

	/*alert ("Bobj="+Bobj);*/
	
	Bobj.style.fontSize="11px";
/*  alert ("Bobj.style.fontSize="+Bobj.style.fontSize);*/
	
    Bobj.style.color=color[id];
/*	alert ("Bobj.style.color="+Bobj.style.color);*/
	
    Bobj.style.width=sWidth + "px";
/*	alert ("Bobj.style.width="+Bobj.style.width);*/
	
    Bobj.style.height=sHeight + "px";
/*	alert ("Bobj.style.height="+Bobj.style.height);*/
	
    Bobj.style.lineHeight=sHeight + "px";
	/*alert ("Bobj.style.lineHeight="+Bobj.style.lineHeight);*/
	
    Bobj.style.background="url(" + bImg + ") no-repeat left " + len[id] + "px";
	/*alert ("Bobj.style.background="+Bobj.style.background);*/
	
    Bobj.style.textIndent="20px";
/*	alert ("Bobj.style.textIndent="+Bobj.style.textIndent);*/
	
    Bobj.innerHTML="检测提示：" + msg[id];
/*	alert ("Bobj.style.innerHTML="+Bobj.style.innerHTML);*/
	if(id < 2) {
		/*alert (id);*/
		return false;
	}
	else return true;
}


function login_chk(login_ob){
	set_op(login_ob); 
	var in_name = $("#acc_name")[0].value;
	var in_pwd = $("#pwd1")[0].value;
	
	var x = document.getElementsByName("cook_t");
	
	var cook_t = 0;
	
	var verify = $("#verify")[0].value;
	
	for(i = 0; i < x.length; i++){
		if(x[i].checked == true){
			cook_t = x[i].value;
		}
		i++;
	}

	//alert (cook_t);
	/*alert ($("#oper")[0].value);*/
	
	var log_result = "";
	if($("#oper")[0].value == "log"){
		if(in_name == "" ){
			$("#result")[0].innerHTML = "用户名或密码不能为空！";
			return false;
		}
		else if (verify == ""){
			$("#result")[0].innerHTML = "验证码不能为空！";
			return false;
		}
		else{
			$.post('checkuser',{acc_name:in_name,pwd:in_pwd,verify:verify},
				function (s){
					var re = eval("("+s+")");
					//alert (re['status']);
					
					if(re['status'] == "0"){
						$("#result")[0].innerHTML = re['info'];
						$('#verifyImg').attr("src",re['data']+new Date().getTime());
						//alert ($('#verifyImg').attr("src"));
						return false;
					}
					else if(re['status'] == "1"){
						$("#result")[0].innerHTML = re['info'];
						self.location = re['data'];
					}
					
/*					if(s == "1") {
						//alert ("success!");
						window.location = "__ROOT__/index.php";
					}
					else if (s == "2") {
						$("#result")[0].innerHTML = "用户名或密码错误！";
						return false;
					}
					else if (s == "3"){
						$("#result")[0].innerHTML = "验证码错误！";
						return false;
					}
					else{
						return false;
					}*/
				}
			);
			
		}
	}
	else {
		
		self.location = "reg";
	}
	return false;
	
}

function edit_chk(){
	
	if(!pwd1_sub()){
		alert ("密码长度不符合要求!");
		return false;
	}
	
	if(!chkAgain()){ 
		alert ("密码确认不符合要求!");
		return false;
	}
	if(!areaCHK()){
		alert("请选择地区!");
		return false;
	}
	else{
		var mid = document.getElementById('acc_id').value;
		var mname = document.getElementById('acc_name').value;
		//var opwd = document.getElementById('pwd_old').value;
		var npwd = document.getElementById('pwd').value;
		var prov = document.getElementById('province');
		var pname = prov.options[prov.selectedIndex].value;
		var city = document.getElementById('city');
		var cname = city.options[city.selectedIndex].value;
		var email = document.getElementById('email').value;
		
		
		$.post('uc_edit',{edit_btn:"1", uid:mid,acc_name:mname, pwd:npwd, email:email, pro_id:pname, city_id:cname},function(s){
			s = eval("("+s+")");
			if(s['status'] == "1"){
				alert(s['info']);
				self.location = s['data'];
			}
			else{
				$("#old_pwd")[0].innerHTML = "用户名或密码错误！";
				return false;
			}
			
		});
	}
	
	
	
}




	
function logout(){
	top.location = "logout";
}

function newsCHK(){

	if(!document.getElementById('news_title').value){
		alert ("请填写新闻标题!");
		return false;
	}
	
	if(document.getElementById('select_cato').value == 0){
		alert ("请选择分类!");
		return false;
	}

	return true;
}

function catoCHK(){

	if(!document.getElementById('cato_name').value){
		alert ("请填写分类名称!");
		return false;
	}
	return true;
}





function reset_pwd(){
	//alert (1);
	var res_name = $("#res_name")[0].value;
	var res_email = $("#res_email")[0].value;
	var res_pwd = $("#new_pwd")[0].value;
	//alert (res_name+"   "+res_email);
	

	
	//var log_result = "";
	//if($("#repwd_sub")[0].value == "log"){
		if(res_name == "" || res_email == ""){
			$("#res_text")[0].innerHTML = "用户名或邮箱不能为空！";
			return false;
		}
		else{
			$.post('index.php?c=user&a=pwd_reset',{res_name:res_name,res_email:res_email,new_pwd:res_pwd,repwd_sub:1},function (s){

																				if(s == "1") {
																					$("#res_text")[0].innerHTML = "密码修改成功！";
																					setTimeout(self.location = "index.php",3000);
																					exit;
																				}
																				else {
																					$("#res_text")[0].innerHTML = "用户名或邮箱错误！";
																				}
																				
																	});
		}
/*	}
	else {
		self.location = "me.php?oper=reg";
	}*/
}



function area(d){
	$url = document.getElementById('url').value+"Area/area_proc"
	//alert ($url);
	$.post($url,{id:d},
		function(s){
			//alert (d);
			var prov_arr = eval("("+s+")"); 
			//alert (prov_arr['data']);
			Init_select(d,prov_arr['data']);
			
			
		})
	
}

function Init_select(sd,arr){
	var i = 1;
	var id = "";
	var id2 = "";
	//alert(arr);
	if(sd !="0" ) {
		id = "city";
		id2 = "city_name";
	}
	else {
		id = "province";
		id2 = "pro_name";
	}
	
	var x = document.getElementById(id);
	var l = document.getElementById(id).length;
	
//	if(arr != ""){
		while (arr[i-1]){
	/*		alert (id);*/
	/*		alert (arr[i-1]);*/
	//		alert ($(id).length);
			x.length = i+1;
			//alert (arr[i-1][0]);
			x.options[i].value = arr[i-1][0];
			x.options[i].text = arr[i-1][1];
			i++;
		}
		
//	}
	if(sd == "0"){
		document.getElementById("city").length = 1;
	}


}

function get_city(ss){
	//alert (ss.selectedIndex);
	var prov_id = ss.value;
	//alert (prov_id);
	area(prov_id);

}

function getArea(area_ob){
	var Id_str = "";
	if(area_ob.id == "province"){
		Id_str = "pro_name";
	}
	else if(area_ob.id == "city"){
		Id_str = "city_name";
	}
	else{
		Id_str = "";
	}
	//alert (area_ob.options[area_ob.selectedIndex].text);
	document.getElementById(Id_str).value = area_ob.options[area_ob.selectedIndex].text;
}

function areaCHK(){
	var prov_chk = document.getElementById('province').value;
	var city_chk = document.getElementById('city').value;
	if(prov_chk=="0" || city_chk== "0"){
		return false;
	}
	return true;
}
	

function fleshVerify(img,type){ 
	//重载验证码,用jquery,语法为 $('#id').attr("src",url);
	//alert ($('#verifyImg').attr("src"));
	//alert (img.src);
	var timenow = new Date().getTime();
	//alert (img+'/Public/verify/'+timenow);
	if (type){
		$('#verifyImg').attr("src", img+'/Public/verify/adv/1/'+timenow);
		
	}else{
		$('#verifyImg').attr("src",img+'/Public/verify/'+timenow);
		
	}
	//alert ($('#verifyImg').style.SRC);
}

function verifyCHK(){
	var verify_chk = document.getElementById('verify').value;
	if(verify_chk == ""){
		return false;
	}
	return true;
}

function show_post(p_url){
		
		if(arguments.length   ==   0) {
			p_url = 0;
		}
		
		x = document.getElementById("g_post");
		t = document.getElementById("g_table");
		//alert (x.style.display);
/*		if(contents){
			//alert (contents)
			t_len = t.rows.length;
			t.insertRow(2);
			t.rows[2].insertCell(0);
			t.rows[2].cells[0].innerHTML = "<div class='post'><h2 class='title'>"+contents+"</h2><p class='meta'>由  在  发布。</p></div>";
			document.getElementById("contents").value = "";
		}*/
		
		
		if(x.style.display == "" || x.style.display == "none"){
			x.style.display = "block";
			KE.create('contents');    
		}
		else if(x.style.display == "block"){
			incontents = $("#contents").val();
			KE.remove('contents');
			x.style.display = "none";
			if(incontents){
				//alert(incontents);
				$.post(p_url+"/Post/p_post",{contents:incontents},function(s){
					re = eval("("+s+")");
					if(re['status']== 1){
						/*t_len = t.rows.length;
						t.insertRow(2);
						t.rows[2].insertCell(0);
						t.rows[2].cells[0].innerHTML = "<div class='post'><h2 class='title'>"+incontents+"</h2><p class='meta'>由 <b><span class='guest_name'>"+re['data']['acc_name']+"</span></b> 在 "+re['data']['gtime']+" 发布。</p></div>";
						armoryinfo();*/
						gbookshow();
						//document.getElementById("contents").value = "";
						if($("#noguest")){
							$("#noguest").css("display") = "none";
						}
						
					}
					else{
						alert(re['info']);
					}
					
				});
			}
		}
	}
	
	
function gquit(gname){
	
	var r = confirm("确定退出公会么？");
	if(r==true){
		
		self.location = 'gquit/acc_name/'+gname;
	}
	else{
		return false;
	}
}